import { createApp } from 'vue'
import LoadingComponent from './Loading/index.vue'

class LoadingInstance {
  constructor() {
    // 创建一个包裹元素
    this.container = document.createElement('div')
    // 创建组件实例
    const app = createApp(LoadingComponent)
    this.instance = app.mount(this.container)
    // 请求计数器
    this.requestCount = 0
    // 将容器添加到 body
    document.body.appendChild(this.container)
  }

  show() {
    this.requestCount++
    if (this.requestCount > 0) {
      this.instance.show()
    }
  }

  hide() {
    this.requestCount--
    if (this.requestCount <= 0) {
      this.instance.hide()
      this.requestCount = 0 // 确保计数不会为负
    }
  }
}

// 导出单例
export default new LoadingInstance() 